"""
Case Type   : cm
Case Name   : cm_ctl list查看cm_agent或cm_server参数基本参数验证
Create At   : 2024-07
Owner       : haomeng
Description :
    1、查看集群状态
    2、查看并列出cm_agent所有参数
    3、查看并列出cm_server所有参数
    4、指定多个参数
    5、指定错误参数
Expect      :
    1、集群所有节点状态正常，显示Normal
    2、成功，单节点默认参数数量为40
    3、成功，单节点默认参数数量为60
    4、以后一个为准
    5、合理报错
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class CmCtl(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_cm(self):
        text = '----step1:查看集群状态; expect:集群状态正常,DN主在节点1----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn("cluster_state   : Normal", status)

        text = '----step2:查看并列出cm_agent所有参数; expect:成功，单节点默认参数数量为40----'
        self.log.info(text)

        agent_param = []
        agent_cmd = f'''source {macro.DB_ENV_PATH};cm_ctl list --param --agent;'''
        self.log.info(agent_cmd)
        res1 = self.pri_user.sh(agent_cmd).result()
        for param in res1.splitlines():
            agent_param.append(param)
        self.log.info(agent_param)
        self.assertEqual(120, len(agent_param), 'agent参数查看失败')

        text = '----step3:查看并列出cm_server所有参数  expect:成功，单节点默认参数数量为60----'
        self.log.info(text)

        server_param = []
        server_cmd = f'''source {macro.DB_ENV_PATH};cm_ctl list --param --server;'''
        self.log.info(server_cmd)
        res2 = self.pri_user.sh(server_cmd).result()
        for param in res2.splitlines():
            server_param.append(param)
        self.log.info(server_param)
        self.assertEqual(180, len(server_param), 'server参数查看失败')

        text = '----step4:指定多个参数  expect:成功，以后一个参数为准----'
        self.log.info(text)

        param_list = []
        cmd = f'''source {macro.DB_ENV_PATH};cm_ctl list --param --server --agent;'''
        self.log.info(cmd)
        res3 = self.pri_user.sh(cmd).result()
        for param in res3.splitlines():
            param_list.append(param)
        self.log.info(param_list)
        self.assertEqual(120, len(param_list), '参数查看失败')

        text = '----step5:指定错误参数  expect:合理报错----'
        self.log.info(text)

        param_list = ['', '--param', '--server', '--agent']
        for param in param_list:
            cmd = f'''source {macro.DB_ENV_PATH};cm_ctl list {param};'''
            self.log.info(cmd)
            res5 = self.pri_user.sh(cmd).result()
            self.assertIn('command wrong', res5, '参数查看成功')

    def tearDown(self):
        self.log.info('-----No need to clean-----')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
